#ifndef _PLAYLISTMODEL_H_
#define _PLAYLISTMODEL_H_

#include <QApplication>
#include <QListView>
#include <QAbstractListModel>
#include <QVariant>
#include <QString>
#include <QVector>
#include <QPair>
#include <QTime>
#include <QWidget>
#include <QLabel>
#include <QHBoxLayout>
#include <QStyledItemDelegate>
#include <QPainter>

struct PlaylistItemModel 
{
    QString name;
    double duration;
};

class PlaylistModel : public QAbstractListModel 
{
    Q_OBJECT
public:
    enum PlaylistRoles {
        FileNameRole = Qt::UserRole + 1,
        DurationRole
    };

    PlaylistModel(QObject *parent = nullptr);

    void setPlaylist(const QVector<PlaylistItemModel>& playlist) {
        beginResetModel();
        m_playlist = playlist;
        endResetModel();
    }

    int rowCount(const QModelIndex &parent = QModelIndex()) const override {
        Q_UNUSED(parent);
        return m_playlist.size();
    }

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    QHash<int, QByteArray> roleNames() const override {
        QHash<int, QByteArray> roles;
        roles[FileNameRole] = "fileName";
        roles[DurationRole] = "duration";
        return roles;
    }

private:
    QVector<PlaylistItemModel> m_playlist; // 文件名和时长
};

class PlaylistItemDelegate : public QStyledItemDelegate {
    Q_OBJECT
public:
    explicit PlaylistItemDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override ;

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
               const QModelIndex &index);
    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const override;
};

#endif